package com.jukte.recipe.adapters;

import java.util.List;

import com.ewhat.recipes.R;
import com.jukte.recipe.models.AboutResource;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ResourceGridAdapter extends BaseAdapter {
	private List<AboutResource> resources;
	private Activity activity;

	public ResourceGridAdapter(Activity context, int textViewResourceId,
			List<AboutResource> items) {
		this.resources = items;
		this.activity = context;
	}

	public static class ViewHolder {
		public ImageView imgView;
		public TextView txtViewTitle;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder view;
		LayoutInflater inflator = activity.getLayoutInflater();

		if (convertView == null) {
			view = new ViewHolder();
			convertView = inflator.inflate(R.layout.item_resource_grid, null);

			view.txtViewTitle = (TextView) convertView
					.findViewById(R.id.textView1);
			view.imgView = (ImageView) convertView
					.findViewById(R.id.imageView1);

			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}

		view.txtViewTitle.setText(resources.get(position).getText());
		
		int resId = activity.getResources().getIdentifier("quinoa_menuicon","raw", activity.getPackageName());
		Bitmap bmp = BitmapFactory.decodeFile(activity.getFilesDir().getPath()+"/raw/"+resources.get(position).getImagePath()+".png");
		if(null != bmp){
			view.imgView.setImageBitmap(bmp);
		}else{
			view.imgView.setImageResource(resId);
		}
		return convertView;
	}

	@Override
	public int getCount() {
		return resources.size();
	}

	@Override
	public Object getItem(int arg0) {
		return resources.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
}